package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    @Transactional
    public void addWitnFlavor(DishDto dishDto) {
        //将提交的数据存入dish表
        this.save(dishDto);
        //获取提交数据中的flavor 口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //判断flavors是否有数据,有的话
        if (flavors != null && flavors.size() > 0){
            //获取dishId,跟口味数据flavor一起封装到DishFlavor表中
            Long dishId = dishDto.getId();
            //遍历flavors集合
            for (DishFlavor flavor : flavors) {
                //设置dishId
                flavor.setDishId(dishId);
            }
            //存储数据到dish_flavor表中
            dishFlavorService.saveBatch(flavors);
        }

    }
}
